//
// Created by sunhongda on 2017/06/16
//
#ifndef WIN32
#include "com_chuangmi_mp4_ImiVideoSynthesis.h"
#include "imimp4_ffmpeg_api.h"
#include "log.h"


#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_chuangmi_mp4_ImiVideoSynthesis
 * Method:    initMp4
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_chuangmi_mp4_ImiVideoSynthesis_initMp4
  (JNIEnv *pJNIEnv, jobject object)
{
    LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_initMp4\n");
    IMIMp4FFmpeg_Init();
	return (jint)0;
}

/*
 * Class:     com_chuangmi_mp4_ImiVideoSynthesis
 * Method:    createFile
 * Signature: (Ljava/lang/String;IIIIIIII[I)I
 */
JNIEXPORT jint JNICALL Java_com_chuangmi_mp4_ImiVideoSynthesis_createFile
  (JNIEnv *pJNIEnv,
  jobject object,
  jstring path,
  jint container,
  jint video,
  jint audio,
  jint vfps,
  jint vwidth,
  jint vheight,
  jint achannel,
  jint asamplerate,
  jintArray pIMIHandle)
{
	const char* p = pJNIEnv->GetStringUTFChars(path, 0);
	unsigned int c = (unsigned int)container;
	unsigned int v = (unsigned int)video;
	unsigned int a = (unsigned int)audio;
	unsigned int ivfs = (unsigned int)vfps;
	unsigned int ivwidth = (unsigned int)vwidth;
	unsigned int ivheight = (unsigned int)vheight;
	unsigned int iachannel = (unsigned int)achannel;
	unsigned int iasamplerate = (unsigned int)asamplerate;
	jboolean b;
	jint* handle = pJNIEnv->GetIntArrayElements(pIMIHandle, &b);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_createFile begin\n");
	int r = IMIMp4FFmpeg_Create_File(p, c, v, a, ivfs, ivwidth, ivheight, iachannel, iasamplerate, (IMIMp4FFmpegHandle*)handle);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_createFile end\n");
	pJNIEnv->ReleaseIntArrayElements(pIMIHandle, handle, 0);
	return (jint)r;
}

/*
 * Class:     com_chuangmi_mp4_ImiVideoSynthesis
 * Method:    writeVideoFrameFile
 * Signature: ([ILjava/nio/ByteBuffer;IIZ)I
 */
JNIEXPORT jint JNICALL Java_com_chuangmi_mp4_ImiVideoSynthesis_writeVideoFrameFile
  (JNIEnv *pJNIEnv, jobject object, jintArray pIMIHandle, jobject buff, jint bufflen, jint vtimestamp, jboolean iskeyframe)
{
	jboolean b;
	jint* handle = pJNIEnv->GetIntArrayElements(pIMIHandle, &b);
	unsigned char* video_data = (unsigned char*)pJNIEnv->GetDirectBufferAddress(buff);
	unsigned int video_data_len = (unsigned int)bufflen;
	unsigned int video_timestamp = (unsigned int)vtimestamp;
	bool key = (bool)iskeyframe;
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_writeVideoFrameFile begin\n");
	int r = IMIMp4FFmpeg_WriteVideoFrame((IMIMp4FFmpegHandle)*handle, video_data, video_data_len, video_timestamp, key);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_writeVideoFrameFile end\n");
	pJNIEnv->ReleaseIntArrayElements(pIMIHandle, handle, 0);
	return (jint)r;
}

/*
 * Class:     com_chuangmi_mp4_ImiVideoSynthesis
 * Method:    writeAudioFrameFile
 * Signature: ([ILjava/nio/ByteBuffer;II)I
 */
JNIEXPORT jint JNICALL Java_com_chuangmi_mp4_ImiVideoSynthesis_writeAudioFrameFile
  (JNIEnv *pJNIEnv, jobject object, jintArray pIMIHandle, jobject buff, jint bufflen, jint atimestamp)
{
	jboolean b;
	jint* handle = pJNIEnv->GetIntArrayElements(pIMIHandle, &b);
	unsigned char* audio_data = (unsigned char*)pJNIEnv->GetDirectBufferAddress(buff);
	unsigned int audio_data_len = (unsigned int)bufflen;
	unsigned int audio_timestamp = (unsigned int)atimestamp;
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_writeAudioFrameFile begin\n");
	int r = IMIMp4FFmpeg_WriteAudioFrame((IMIMp4FFmpegHandle)*handle, audio_data, audio_data_len, audio_timestamp);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_writeAudioFrameFile end\n");
	pJNIEnv->ReleaseIntArrayElements(pIMIHandle, handle, 0);
	return (jint)r;
}

/*
 * Class:     com_chuangmi_mp4_ImiVideoSynthesis
 * Method:    closeFile
 * Signature: ([I)I
 */
JNIEXPORT jint JNICALL Java_com_chuangmi_mp4_ImiVideoSynthesis_closeFile
  (JNIEnv *pJNIEnv, jobject object, jintArray pIMIHandle)
{
	jboolean b;
	jint* handle = pJNIEnv->GetIntArrayElements(pIMIHandle, &b);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_closeFile begin\n");
	int r = IMIMp4FFmpeg_CloseFileForCreate((IMIMp4FFmpegHandle)*handle);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_closeFile end\n");
	pJNIEnv->ReleaseIntArrayElements(pIMIHandle, handle, 0);
	return (jint)r;
}

/*
 * Class:     com_chuangmi_mp4_ImiVideoSynthesis
 * Method:    openFile
 * Signature: (Ljava/lang/String;[I[I[I[I[I[I[I[I[I)I
 */
JNIEXPORT jint JNICALL Java_com_chuangmi_mp4_ImiVideoSynthesis_openFile
  (JNIEnv *pJNIEnv,
  jobject object,
  jstring path,
  jintArray container,
  jintArray video,
  jintArray audio,
  jintArray vfps,
  jintArray vwidth,
  jintArray vheight,
  jintArray achannel,
  jintArray asamplerate,
  jintArray abitrate,
  jintArray pIMIHandle)
{

	const char* p = pJNIEnv->GetStringUTFChars(path, 0);
	jboolean b;
	jint* icontainer = pJNIEnv->GetIntArrayElements(container, &b);
	jint* ivideo = pJNIEnv->GetIntArrayElements(video, &b);
	jint* iaudio = pJNIEnv->GetIntArrayElements(audio, &b);
	jint* ivfps = pJNIEnv->GetIntArrayElements(vfps, &b);
	jint* ivwidth = pJNIEnv->GetIntArrayElements(vwidth, &b);
	jint* ivheight = pJNIEnv->GetIntArrayElements(vheight, &b);
	jint* iachannel = pJNIEnv->GetIntArrayElements(achannel, &b);
	jint* iasamplerate = pJNIEnv->GetIntArrayElements(asamplerate, &b);
	jint* iabitrate = pJNIEnv->GetIntArrayElements(abitrate, &b);
	jint* handle = pJNIEnv->GetIntArrayElements(pIMIHandle, &b);

	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_openFile begin\n");
	int r = IMIMp4FFmpeg_Open_File(p,
			(unsigned int*)icontainer,
			(unsigned int*)ivideo,
			(unsigned int*)iaudio,
			(unsigned int*)ivfps,
			(unsigned int*)ivwidth,
			(unsigned int*)ivheight,
			(unsigned int*)iachannel,
			(unsigned int*)iasamplerate,
			(unsigned int*)iabitrate,
			(IMIMp4FFmpegHandle*)handle);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_openFile end\n");
	pJNIEnv->ReleaseIntArrayElements(container, icontainer, 0);
	pJNIEnv->ReleaseIntArrayElements(video, ivideo, 0);
	pJNIEnv->ReleaseIntArrayElements(audio, iaudio, 0);
	pJNIEnv->ReleaseIntArrayElements(vfps, ivfps, 0);
	pJNIEnv->ReleaseIntArrayElements(vwidth, ivwidth, 0);
	pJNIEnv->ReleaseIntArrayElements(vheight, ivheight, 0);
	pJNIEnv->ReleaseIntArrayElements(achannel, iachannel, 0);
	pJNIEnv->ReleaseIntArrayElements(asamplerate, iasamplerate, 0);
	pJNIEnv->ReleaseIntArrayElements(abitrate, iabitrate, 0);
	pJNIEnv->ReleaseIntArrayElements(pIMIHandle, handle, 0);
	return (jint)r;
}

/*
 * Class:     com_chuangmi_mp4_ImiVideoSynthesis
 * Method:    getVideoFrame
 * Signature: ([I[B[I[IZZ)I
 */
JNIEXPORT jint JNICALL Java_com_chuangmi_mp4_ImiVideoSynthesis_getVideoFrame
  (JNIEnv *pJNIEnv, jobject object, jintArray pIMIHandle, jbyteArray data, jintArray data_len, jintArray timestamp, jintArray iskeyframe, jintArray isaudioframe)
{
	jboolean b;
	jint* handle = pJNIEnv->GetIntArrayElements(pIMIHandle, &b);
	jbyte* pdata = pJNIEnv->GetByteArrayElements(data, &b);
	jint* ilen = pJNIEnv->GetIntArrayElements(data_len, &b);
	jint* itimestamp = pJNIEnv->GetIntArrayElements(timestamp, &b);
	jint* iiskeyframe = pJNIEnv->GetIntArrayElements(iskeyframe, &b);
	jint* iisaudioframe = pJNIEnv->GetIntArrayElements(isaudioframe, &b);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_getVideoFrame begin\n");
	int r = IMIMp4FFmpeg_GetVideoFrame((IMIMp4FFmpegHandle)*handle,
			(unsigned char*)pdata,
			(unsigned int*)ilen,
			(unsigned int*)itimestamp,
			(bool*)iiskeyframe,
			(bool*)iisaudioframe);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_getVideoFrame end\n");
	pJNIEnv->ReleaseIntArrayElements(pIMIHandle, handle, 0);
	pJNIEnv->ReleaseByteArrayElements(data, pdata, 0);
	pJNIEnv->ReleaseIntArrayElements(data_len, ilen, 0);
	pJNIEnv->ReleaseIntArrayElements(timestamp, itimestamp, 0);
	pJNIEnv->ReleaseIntArrayElements(iskeyframe, iiskeyframe, 0);
	pJNIEnv->ReleaseIntArrayElements(isaudioframe, iisaudioframe, 0);
	return (jint)r;
}

/*
 * Class:     com_chuangmi_mp4_ImiVideoSynthesis
 * Method:    closeFileForOpen
 * Signature: ([I)I
 */
JNIEXPORT jint JNICALL Java_com_chuangmi_mp4_ImiVideoSynthesis_closeFileForOpen
  (JNIEnv *pJNIEnv, jobject object, jintArray pIMIHandle)
{
	jboolean b;
	jint* handle = pJNIEnv->GetIntArrayElements(pIMIHandle, &b);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_closeFileForOpen begin\n");
	int r = IMIMp4FFmpeg_CloseFileForOpen((IMIMp4FFmpegHandle)*handle);
	LOGE("Java_com_chuangmi_mp4_ImiVideoSynthesis_closeFileForOpen end\n");
	pJNIEnv->ReleaseIntArrayElements(pIMIHandle, handle, 0);
	return (jint)r;
}

#ifdef __cplusplus
}
#endif
#endif